package com.ylq.reggie_take_away.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ylq.reggie_take_away.common.R;
import com.ylq.reggie_take_away.entity.Employee;
import com.ylq.reggie_take_away.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @PostMapping("/login")
    public R<Employee> login(HttpServletRequest request, @RequestBody Employee employee) {

        //md5加密页面提交的密码
        String password = employee.getPassword();
        password= DigestUtils.md5DigestAsHex(password.getBytes());

        //查数据库
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Employee::getUsername,employee.getUsername());
        Employee emp = employeeService.getOne(queryWrapper);

        //数据为空
        if(emp == null){
            return R.error("登录失败");
        }

        //密码错误
        if(!emp.getPassword().equals(password)){
            return R.error("登录失败");
        }

        // 获取状态
        if (emp.getStatus() == 0){
            return R.error("账号已禁用");
        }

        //登录成功，员工id存入session并返回成功结果
        request.getSession().setAttribute("employee",emp.getId());
        return R.success(emp);

    }

    //清除session,退出登录
    @PostMapping("/logout")
    public R<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("employee");
        return R.success("退出成功");
    }

    /**
     * 新增员工
     * @param request
     * @param employee
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody Employee employee) {
        log.info("新增员工,员工信息: {}",employee.toString());

        //设置初始密码,更新时间创建人和更新人
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
        employee.setCreateTime(LocalDateTime.now());
        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setCreateUser(empId);
        employee.setUpdateUser(empId);
        // 调用保存方法
        employeeService.save(employee);
        return R.success("更新成功");
    }

    /**
     * 员工信息分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name) {
        log.info("page: {},pageSize: {},name: {}",page,pageSize,name);

        //分页构造器
        Page pageinfo = new Page<>(page,pageSize);

        //条件构造器
        LambdaQueryWrapper<Employee> queryWrapper = new LambdaQueryWrapper();

        //过滤条件,name==null则不添加
        queryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);

        //排序
        queryWrapper.orderByDesc(Employee::getUpdateTime);

        //执行查询
        employeeService.page(pageinfo,queryWrapper);

        return R.success(pageinfo);
    }

    @PutMapping
    public R<String> update(HttpServletRequest request,@RequestBody Employee employee){
        log.info(employee.toString());

        employee.setUpdateTime(LocalDateTime.now());
        Long empId = (Long) request.getSession().getAttribute("employee");
        employee.setUpdateUser(empId);
        employeeService.updateById(employee);
        return R.success("修改员工信息成功");
    }

    /**
     * 根据id查询员工信息，用于回写到修改页面
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public R<Employee> getById(@PathVariable Long id){
        log.info("查询员工id: {}",id);
        Employee employee = employeeService.getById(id);
        if(employee == null){
            return R.error("没有查到员工信息");
        }
        return R.success(employee);
    }
}
